//Documentation complete
/**
 * @author Kazu
 * @version 2009FE09
 */

package problemDomain;

import static problemDomain.ErrorBean.throwError;
import static problemDomain.ErrorBean.ErrorCode.*;
import static utilities.XMLTools.encodeEntities;

/**
 * A patron who can order food but does not have a persistent login account.
 */
public class PatronAccountBean implements Bean {
	
	/* Constants */
	
	private static final long serialVersionUID = -8641277205012474794L;
	
	/* END Constants */
	
	
	/* Attributes */
	
	protected int id;
	protected String firstname;
	protected String lastname;
	protected String phone;
	
	/* END Attributes */
	
	
	/* Constructors */
	
	/**
	 * Constructs a PatronAccountBean object with default values.
	 */
	public PatronAccountBean () {
		id = 0;
		phone = "";
		firstname = "";
		lastname = "";
	}
	
	/**
	 * Constructs a PatronAccountBean object with specified values.
	 * @param id the ID of the patron account
	 * @param firstname the first name of the patron
	 * @param lastname the last name of the patron
	 * @param phone the phone number of the patron
	 */
	public PatronAccountBean (
			int id, String firstname,
			String lastname, String phone) {
		this.id = id;
		this.firstname = firstname;
		this.lastname = lastname;
		this.phone = phone;
	}
	
	/* END Constructors */
	
	
	/* Accessors */
	
	/**
	 * Returns the ID of the patron.
	 * @return the ID of the patron
	 */
	public int getId () {
		return id;
	}
	
	/**
	 * Returns the first name of the patron.
	 * @return the first name of the patron
	 */
	public String getFirstname () {
		return firstname;
	}
	
	/**
	 * Returns the last name of the patron.
	 * @return the last name of the patron
	 */
	public String getLastname () {
		return lastname;
	}
	
	/**
	 * Returns the phone number of the patron.
	 * @return the phone number of the patron
	 */
	public String getPhone () {
		return phone;
	}
	
	/* END Accessors */
	
	
	/* Mutators */
	
	/**
	 * Sets the ID of the patron.
	 * @param id the ID of the patron
	 */
	public void setId (int id) {
		this.id = id;
	}
	
	/**
	 * Sets the first name of the patron.
	 * @param firstname the first name of the patron
	 */
	public void setFirstname (String firstname) {
		this.firstname = firstname;
	}
	
	/**
	 * Sets the last name of the patron.
	 * @param lastname the last name of the patron
	 */
	public void setLastname (String lastname) {
		this.lastname = lastname;
	}
	
	/**
	 * Sets the phone number of the patron.
	 * @param phone the phone number of the patron
	 */
	public void setPhone (String phone) {
		this.phone = phone;
	}
		
	/* END Mutators */
	
	
	/* (Bean) Methods */
	
	@Override
	public boolean isValid () throws ErrorBean {
		if (id < 0)
			throwError(
				PATRON_ID_INVALID,
				String.valueOf(id),
				this);
		
		if (phone != null && !phone.matches("\\d\\d\\d\\d\\d\\d\\d\\d\\d\\d"))
			throwError(
				PATRON_PHONE_NUMBER_INVALID,
				phone,
				this);
		
		if (firstname != null && firstname.length() > 30)
			throwError(
				PATRON_FIRST_NAME_INVALID,
				firstname,
				this);
		
		if (lastname != null && lastname.length() > 30)
			throwError(
				PATRON_LAST_NAME_INVALID,
				lastname,
				this);
		
		return true;
	}

	@Override
	public String toXML () {
		StringBuilder sb = new StringBuilder();
		sb.append("<patron>");
		sb.append("<id>" + id + "</id>");
		sb.append("<firstname>" + (firstname != null ? firstname : "") + "</firstname>");
		sb.append("<lastname>" + (phone != null ? lastname : "") + "</lastname>");
		sb.append("<phone>" + (phone != null ? phone : "") + "</phone>");
		sb.append("</patron>");
		return encodeEntities(sb.toString());
	}
	
	/* END (Bean) Methods */
	
	
	/* (Object) Methods */
	
	@Override
	public String toString () {
		StringBuilder sb = new StringBuilder();
		sb.append("ID: " + id + '\n');
		sb.append("First Name: " + firstname + '\n');
		sb.append("Last Name: " + lastname + '\n');
		sb.append("Phone: " + phone + '\n');
		return sb.toString();
	}
	
	/* END (Object) Methods */
	
}
